package com.module.frame.ext

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.time.Duration

/**
 * 简易封装 shareFlow发送数据跟监听数据
 * 因为都需要在协程上处理，嫌弃麻烦，就封装起来
 */

/**
 * shareFlow发送数据
 */
fun <T> FragmentActivity.postEvent(
    shareFlow: MutableSharedFlow<T>,
    data: T
) {
    postEvent(lifecycleScope, shareFlow, data)
}

/**
 * shareFlow发送数据
 */
fun <T> Fragment.postEvent(
    shareFlow: MutableSharedFlow<T>,
    data: T
) {
    postEvent(lifecycleScope, shareFlow, data)
}

/**
 * shareFlow发送数据
 */
fun <T> ViewModel.postEvent(
    shareFlow: MutableSharedFlow<T>,
    data: T?
) {
    postEvent(viewModelScope, shareFlow, data)
}


/**
 * shareFlow发送数据
 */
fun <T> postEvent(
    shareFlow: MutableSharedFlow<T>,
    data: T
) {
    postEvent(GlobalScope, shareFlow, data)
}

/**
 * shareFlow发送数据
 */
fun <T> postEvent(
    scope: CoroutineScope,
    shareFlow: MutableSharedFlow<T>,
    data: T?
) {
    scope.launch {
        data?.let {
            shareFlow.emit(it)
        }
    }
}

/**
 * shareFlow发送数据
 */
fun <T> FragmentActivity.observe(
    shareFlow: MutableSharedFlow<T>,
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    delay: Long = 0,//延迟单位毫秒
    success: (T) -> Unit
) {
    observe(lifecycleScope, lifecycle, shareFlow, delay, state, success)
}

/**
 *  MutableSharedFlow 事件监听
 */
fun <T> Fragment.observe(
    shareFlow: MutableSharedFlow<T>,
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    delay: Long = 0,//延迟单位毫秒
    success: (T) -> Unit
) {
    observe(lifecycleScope, viewLifecycleOwner, shareFlow, delay, state, success)
}

/**
 *  MutableSharedFlow 事件监听
 */
fun <T> observe(
    scope: CoroutineScope,
    lifeOwner: LifecycleOwner,
    shareFlow: MutableSharedFlow<T>,
    delay: Long = 0,//延迟单位毫秒
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    success: (T) -> Unit
) {
    scope.launch {
        lifeOwner.repeatOnLifecycle(state) {
            //监听
            shareFlow
                .debounce(delay)
                .collect {
                    success(it)
                }
        }
    }
}

/**
 *  MutableSharedFlow 事件监听
 */
fun <T> observe(
    scope: CoroutineScope,
    lifecycle: Lifecycle,
    shareFlow: MutableSharedFlow<T>,
    delay: Long = 0,//延迟单位毫秒
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    success: (T) -> Unit
) {
    scope.launch {
        lifecycle.repeatOnLifecycle(state) {
            //监听
            shareFlow
                .debounce(delay)
                .collect {
                    success(it)
                }
        }
    }
}

/**
 * shareFlow发送数据
 */
fun <T> FragmentActivity.observeResumed(
    shareFlow: MutableSharedFlow<T>,
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    delay: Long = 0,//延迟单位毫秒
    success: (T) -> Unit
) {
    observeResumed(lifecycleScope, lifecycle, shareFlow, delay, state, success)
}

fun <T> Fragment.observeResumed(
    shareFlow: MutableSharedFlow<T>,
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    delay: Long = 0,//延迟单位毫秒
    success: (T) -> Unit
) {
    observeResumed(lifecycleScope, viewLifecycleOwner, shareFlow, delay, state, success)
}


/**
 *  state虽然能控制Resumed接受，但是对应的onPause就销毁了
 *  所以使用whenResumed，就解决CREATED接受过早，又不会提交销毁等问题
 *
 */
fun <T> observeResumed(
    scope: CoroutineScope,
    lifeOwner: LifecycleOwner,
    shareFlow: MutableSharedFlow<T>,
    delay: Long = 0,//延迟单位毫秒
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    success: (T) -> Unit
) {
    scope.launch {
        lifeOwner.repeatOnLifecycle(state) {
            //监听
            shareFlow
                .debounce(delay)
                .collect {
                    lifeOwner.whenResumed {
                        success(it)
                    }
                }
        }
    }
}

/**
 *  state虽然能控制Resumed接受，但是对应的onPause就销毁了
 *  所以使用whenResumed，就解决CREATED接受过早，又不会提交销毁等问题
 *
 */
fun <T> observeResumed(
    scope: CoroutineScope,
    lifecycle: Lifecycle,
    shareFlow: MutableSharedFlow<T>,
    delay: Long = 0,//延迟单位毫秒
    //默认onCreate时候启动协程，onDestroy时候停止协程
    state: Lifecycle.State = Lifecycle.State.CREATED,
    success: (T) -> Unit
) {
    scope.launch {
        lifecycle.repeatOnLifecycle(state) {
            //监听
            shareFlow
                .debounce(delay)
                .collect {
                    lifecycle.whenResumed {
                        success(it)
                    }
                }
        }
    }
}